package aaaBrowser.test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.attilax.Closure;
import com.attilax.core;
import com.attilax.io.pathx;

public class CopyOfLoginAndPub {

	public static void main(String[] args) {
	 	new CopyOfLoginAndPub().start();
		new CopyOfLoginAndPub().execMeth_AysGui(new Runnable() {
			
			@Override
			public void run() {
				System.out.println("--f");
				
			}
		}, "");
//		
//		core.execMeth_Ays(new Runnable() {
//			
//			@Override
//			public void run() {
//				System.out.println("--aa");
//				
//			}
//		}, "");
	}

	final Display display = new Display();
	atiBrowserExample atiBrowserExampleinstance;

	private void start() {

		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setText("Browser example");
		// shell.setVisible(false);
		// shell.setSize(1, 1);
		atiBrowserExampleinstance = new atiBrowserExample(shell);

		shell.open(); // shell.setVisible(false);

		// core.execMeth_Ays(new Runnable() {
		//
		// @Override
		// public void run() {
		// atiBrowserExampleinstance.addProgressListener(pathx
		// .classPath(LoginAndPub.class) + "\\login.js");
		// atiBrowserExampleinstance
		// .gotoUrl("http://demo.destoon.com/v5.0/member/login.php?a0");
		//
		// }
		// }, "--");

		login();

		// atiBrowserExampleinstance.ini();

		// atiBrowserExampleinstance.addProgressListener(pathx
		// .classPath(LoginAndPub.class) + "\\login.js");

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		// icon.dispose();
		atiBrowserExampleinstance.dispose();
		display.dispose();
	}

	private void login() {

		execMeth_AysGui(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(30000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				atiBrowserExampleinstance.gotoUrl("http://localhost/destoon/member/login.php");
				atiBrowserExampleinstance
						.addProgressListener(
								pathx.classPath(CopyOfLoginAndPub.class)
										+ "\\login.js", new Closure() {

									@Override
									public Object execute(Object arg0)
											throws Exception {

										return null;
									}

								});
				atiBrowserExampleinstance.gotoUrl();

			}
		}, "thrd:: loginner");
		ExecutorService es = Executors.newSingleThreadExecutor();  
		 Future<String> fut=	es.submit(new Callable<String>() {

			@Override
			public String call() throws Exception {
				
				atiBrowserExampleinstance.waitFinish(display);
				System.out.println("after slepp  5s");
				return null;
			}
		});
		 try {
			System.out.println(fut.get());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		pub();

	}

	private void execMeth_AysGui(final Runnable runnable, String string) {
		core.execMeth_Ays(new Runnable() {

			@SuppressWarnings("static-access")
			@Override
			public void run() {
				System.out.println("=-ajsy");
		//		Display default1 = display.getDefault();
			//	runnable.run();
		 	Display.getDefault().asyncExec(runnable);
		//		display.asyncExec(runnable);
			}
		}, string);

	}

	private void pub() {
		core.execMeth_Ays(new Runnable() {

			@Override
			public void run() {
				System.out.println(" start pub");
				try {
					Thread.sleep(8000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				display.getDefault().asyncExec(new Runnable() {

					@Override
					public void run() {

						atiBrowserExampleinstance
								.gotoUrl("http://localhost/destoon/member/my.php?mid=5&action=add&aP4=java");
						atiBrowserExampleinstance.gotoUrl();
						System.out.println(" start pub finish");
					}
				});
			}
		}, "--");

	}

}
